package com.example.easy.excel.demo.utils;

import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.*;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.example.easy.excel.demo.data.*;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.util.Date;
import java.util.List;

public class ParamUtils {

    private ParamUtils(){}

    //DemoData
    public static List<DemoData> getDemoData(){
        List<DemoData> list=ListUtils.newArrayList();
        for (int i=1;i<=10;i++){
            DemoData data=new DemoData();
            data.setNo(i);
            data.setName("路"+i);
            data.setIgnore("xx"+i);
            data.setDate(new Date());
            data.setDoubleDate(0.56);
            list.add(data);
        }
        return list;
    }

    //ComplexHeadData
    public static List<ComplexHeadData> getComplexHeadData(){
        List<ComplexHeadData> list=ListUtils.newArrayList();
        for (int i=1;i<=10;i++){
            ComplexHeadData data=new ComplexHeadData();
            data.setString("标题一"+i);
            data.setDoubleData(i+0.56);
            data.setDate(new Date());
            list.add(data);
        }
        return list;
    }

    //ConverterData
    public static List<ConverterData> getConverterData(){
        List<ConverterData> list=ListUtils.newArrayList();
        for (int i=1;i<=10;i++){
            ConverterData data=new ConverterData();
            data.setString("标题一"+i);
            data.setDoubleData(i+0.56);
            data.setDate(new Date());
            list.add(data);
        }
        return list;
    }

    //WriteCellDemoData
    public static List<WriteCellDemoData> getWriteCellDemoData(){
        WriteCellDemoData data = new WriteCellDemoData();

        // 设置超链接
        WriteCellData<String> hyperlink = new WriteCellData<>("官方网站");
        data.setHyperlink(hyperlink);
        HyperlinkData hyperlinkData = new HyperlinkData();
        hyperlink.setHyperlinkData(hyperlinkData);
        hyperlinkData.setAddress("https://github.com/alibaba/easyexcel");
        hyperlinkData.setHyperlinkType(HyperlinkData.HyperlinkType.URL);

        // 设置备注
        WriteCellData<String> comment = new WriteCellData<>("备注的单元格信息");
        data.setCommentData(comment);
        CommentData commentData = new CommentData();
        comment.setCommentData(commentData);
        commentData.setAuthor("Jiaju Zhuang");
        commentData.setRichTextStringData(new RichTextStringData("这是一个备注"));
        // 备注的默认大小是按照单元格的大小 这里想调整到4个单元格那么大 所以向后 向下 各额外占用了一个单元格
        commentData.setRelativeLastColumnIndex(1);
        commentData.setRelativeLastRowIndex(1);

        // 设置公式
        WriteCellData<String> formula = new WriteCellData<>();
        data.setFormulaData(formula);
        FormulaData formulaData = new FormulaData();
        formula.setFormulaData(formulaData);
        // 将 123456789 中的第一个数字替换成 2
        // 这里只是例子 如果真的涉及到公式 能内存算好尽量内存算好 公式能不用尽量不用
        formulaData.setFormulaValue("REPLACE(123456789,1,1,2)");

        // 设置单个单元格的样式 当然样式 很多的话 也可以用注解等方式。
        WriteCellData<String> writeCellStyle = new WriteCellData<>("单元格样式");
        writeCellStyle.setType(CellDataTypeEnum.STRING);
        data.setWriteCellStyle(writeCellStyle);
        WriteCellStyle writeCellStyleData = new WriteCellStyle();
        writeCellStyle.setWriteCellStyle(writeCellStyleData);
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.
        writeCellStyleData.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 背景绿色
        writeCellStyleData.setFillForegroundColor(IndexedColors.GREEN.getIndex());

        // 设置单个单元格多种样式
        WriteCellData<String> richTest = new WriteCellData<>();
        richTest.setType(CellDataTypeEnum.RICH_TEXT_STRING);
        data.setRichText(richTest);
        RichTextStringData richTextStringData = new RichTextStringData();
        richTest.setRichTextStringDataValue(richTextStringData);
        richTextStringData.setTextString("红色绿色默认");
        // 前2个字红色
        WriteFont writeFont = new WriteFont();
        writeFont.setColor(IndexedColors.RED.getIndex());
        richTextStringData.applyFont(0, 2, writeFont);
        // 接下来2个字绿色
        writeFont = new WriteFont();
        writeFont.setColor(IndexedColors.GREEN.getIndex());
        richTextStringData.applyFont(2, 4, writeFont);

        List<WriteCellDemoData> list = ListUtils.newArrayList();
        list.add(data);
        return list;
    }

    public static List<WidthAndHeightData> getWidthAndHeightData() {
        List<WidthAndHeightData> list = ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            WidthAndHeightData data = new WidthAndHeightData();
            data.setString("路"+i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }

    public static List<DemoStyleData> getDemoStyleData() {
        List<DemoStyleData> list = ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            DemoStyleData data = new DemoStyleData();
            data.setString("路"+i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }
}
